<template>
  <section v-loading="loading">
    <el-button size="mini" @click="addActBtn" type="primary">新建活动</el-button>
    <el-dialog :visible.sync="actEditDialog" :title="currActivity?'编辑 - '+currActivity.title:'新建'" v-if="actEditDialog">
      <act-edit :department="department" :activity="currActivity" v-on:cancel="cancelBtn"></act-edit>
    </el-dialog>
    <el-table :data="activityList">
      <el-table-column label="序号" type="index"></el-table-column>
      <el-table-column label="名称" prop="title" width="150px">
      </el-table-column>
      <el-table-column label="开始" :formatter="formatDate" prop="start">
      </el-table-column>
      <el-table-column label="结束" :formatter="formatDate" prop="end">
      </el-table-column>
      <el-table-column label="设置内容">
        <template slot-scope="scope">
          <el-button type="text" @click="setActDetailBtn(scope.row)">详情</el-button>
          <el-button size="mini" type="text" @click="newShareBtn(scope.row)">分享活动</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button :type="scope.row.edit?'success':'primary'" @click='confirm(scope.row)' size="mini" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
          <el-button @click='del(scope)' size="mini" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination v-show="totalPage>0" style="margin-top:20px;"
                   background
                   layout="prev, pager, next"
                   :page-count="totalPage"
                   @current-change="goPage">
    </el-pagination>
    <el-dialog :visible.sync="actDetailDialog" :title="currActivity.title" v-if="actDetailDialog" width="80%">
      <act-detail :activity="currActivity"  :department="department" v-on:cancel="actDetailDialog=false"></act-detail>
    </el-dialog>

    <el-dialog :visible.sync="newShareDialog" title="新建分享" v-if="currShare">
      <new-share :share="currShare" :department="department.id" v-on:save="saveShareBtn"></new-share>
    </el-dialog>
  </section>
</template>

<script>
  import actEdit from './activityEdit'
  import actDetail from './activityDetail'
  import newShare from '../share&access/newShare'
  import VueResource from 'vue-resource'
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import editNode from './activityNode/activityNode'
  import dateFormater from '@/common/date-formater'
  import errHandler from '@/assets/errHandler'
  export default {
    name: "activityShow",
    components:{editNode,actEdit,actDetail,newShare},
    props: ['department'],
    data() {
      return {
        loading:false,
        totalPage:0,
        activityList:null,
        currActivity:null,
        actEditDialog:false,
        actDetailDialog:false,
        newShareDialog:false,
        currShare:null
      }
    },
    created(){
      this.getActivityList('')
    },
    computed:{
    },
    watch: {
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      cancelBtn(r){
        console.log(r.end)
        this.getActivityList()
        // let index = this.activityList.findIndex(a=>a.id === r.id)
        // if(index>=0){
        //   this.activityList.splice(index,1,r)
        // }else {
        //   this.activityList.unshift(r)
        // }
        // console.log(this.activityList)
        this.actEditDialog=false
      },
      formatDate(row, column, cellValue, index){
        return dateFormater.formatDatetime(cellValue)
      },
      goPage(val){
        this.loading=true
        this.getActivityList(val-1)
      },
      getActivityList(pageNo){
        this.fetch({name:"getActivityListByAdmin",data:{page:pageNo?pageNo:0,departmentId:this.department.id}})
          .then(res=>{
            console.log(res.content)
            this.loading=false
            this.activityList=res.content
            this.totalPage=res.totalPages
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      addActBtn(){
        console.log('跳出编辑/新建dialog')
        this.currActivity=null
        this.actEditDialog=true
        return
      },
      confirm(row){
        this.currActivity=row
        this.actEditDialog=true
      },
      del(scope){//删除按钮
        this.loading=true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name:"deleteActivity",data:{id:scope.row.id}})
              .then(res=>{
                this.loading=false
                this.activityList.splice(scope.$index,1)
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      setActDetailBtn(row){
        console.log(row)
        this.currActivity=row
        this.actDetailDialog=true
      },
      newShareBtn(row){
        console.log('传分享图片地址去-----',row)
        this.currShare={
          contentType:1,
          contentValue:row.id,
          computeType:1,
          title:'活动分享',
          img:row.data.shareImg,
          cId:null,memberId:null,data:{}
        }
        this.newShareDialog=true
      },
      saveShareBtn(e){
        console.log('看看传来了啥',e)
        this.shareActivityByAdmin(e)
      },
      shareActivityByAdmin(data){
        console.log(data)
        this.fetch({name:"shareActivityByAdmin",data})
          .then(res=>{
            console.log(res)

            this.$alert('<!DOCTYPE html><html><body ><img src="'+res.data.wxaCodeUrl+'" style="width: 50%;height: 50%"/></body></html>',
              '所属：'+res.memberName, {
                dangerouslyUseHTMLString: true,
                center: true
              })
            this.newShareDialog=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
    }
  }
</script>

<style scoped>

</style>
